;; The first four lines of this file were added by Dracula.
;; They tell DrScheme that this is a Dracula Modular ACL2 program.
;; Leave these lines unchanged so that DrScheme can properly load this file.
#reader(planet "reader.rkt" ("cce" "dracula.plt") "modular" "lang")
(require "icolorize.lisp")

(require "icommon.lisp")
(require "iimage.lisp")

(module MColorize
  (include-book "avl-rational-keys" :dir :teachpacks)
  (import IImage)
  (import ICommon)
  
  (defun IsAddKeyword(mode)
    (if (or (string-equal mode "ADD") (string-equal mode "A") (string-equal mode "ADDITIVE"))
        t
        nil))
  
  (defun IsMulKeyword(mode)
    (if (or (string-equal mode "MUL") (string-equal mode "M") (string-equal mode "MULTIPLY") (string-equal mode "MULTIPLICATION"))
        t
        nil))
  
  (defun ValidMode(mode)
    (or (IsAddKeyword mode) (IsMulKeyword mode)))
  
  (defun GetNewColor(color colorAdjust mode)
    (let* ((newcolor (if (IsAddKeyword mode)
                         (+ color colorAdjust)
                         (if (IsMulKeyword mode)
                             (* color colorAdjust)
                             (color)))))
           (if (> newColor 255)
               255
               (if (< newColor 0)
                   0
                   (round newColor 1)))))
    
   (defun ColorizePixel (tree rAdjust gAdjust bAdjust x y width height mode)
    (let* ((nextX (getNextXCoordinate x width))
           (nextY (getNextYCoordinate x y width))
           (pixel (getPixel tree x y)))
      (let* ((newR (GetNewColor (car pixel)   rAdjust mode))
             (newG (GetNewColor (cadr pixel)  gAdjust mode))
             (newB (GetNewColor (caddr pixel) bAdjust mode)))
        (if (isValidPixel nextX nextY width height)
            (ColorizePixel (insertPixel tree x y newR newG newB) rAdjust gAdjust bAdjust nextX nextY width height mode)
            (insertPixel tree x y newR newG newB)))))
  
  
  (defun GetErrors(r g b mode)
    (if (ValidMode mode)
        nil
        (list "Invalid colorize mode.")))

  
  (defun MainColorize(tree args)
    (let* ((r      (getElement args 1))
           (g      (getElement args 2))
           (b      (getElement args 3))
           (mode   (getElement args 4))
           (Width  (getWidth tree))
           (Height (getHeight tree))
           (Errors (GetErrors r g b mode)))
      (if (equal Errors nil)
          (mv (ColorizePixel tree r g b 1 Height Width Height mode) (list "Sucessfully colorized image."))
          Errors)))
  
 
        
  
  (export IColorize))